package com.yaosheng.thread.lock;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * Created by yaosheng on 2021/7/1.
 */
public class Test05 {

    volatile List lists = new ArrayList ();

    static Thread t1 = null,t2 = null;

    public void add(Object o){
        lists.add (o);
    }

    public int size(){
        return lists.size ();
    }

    public static void main(String[] args) {

        Test05 test05 = new Test05 ();

        t1 = new Thread ( () -> {

            System.out.println ("t1启动");
            for(int i = 0;i < 10;i ++){
                test05.add (new Object ());
                System.out.println ("add" + i);

                if(test05.size () == 5){
                    LockSupport.unpark (t2);
                    LockSupport.park ();
                }

            }
        },"t1");

        t2 = new Thread ( () -> {

            System.out.println ("t2启动");
            if(test05.size () != 5){
                LockSupport.park ();
            }
            System.out.println ("t2结束");
            LockSupport.unpark (t1);

        },"t2");

        t1.start ();
        t2.start ();
    }
}
